package com.hx.voice.recognition.admin.config;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.hx.voice.recognition.auth.client.interceptor.ServiceAuthRestInterceptor;
import com.hx.voice.recognition.auth.client.interceptor.UserAuthRestInterceptor;
import com.hx.voice.recognition.common.handler.GlobalExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @descripe：admin环境配置插件以及应用和用户拦截
 * @company：南京华讯方舟通讯设备有限公司
 * @author：zhoujinbing
 * @time：2019/6/19 15:37
 * @version：V1.0
 */
@Configuration("voiceAdminWebConfig")
@Primary
public class WebConfiguration implements WebMvcConfigurer {


    /**
     * @description 注入全局异常
     * @method  getGlobalExceptionHandler
     * @param  []
     * @return com.hx.zjb.security.common.handler.GlobalExceptionHandler
     * @date: 2019/6/19 15:39
     * @author:zhoujinbing
     */
    @Bean
    GlobalExceptionHandler getGlobalExceptionHandler()
    {
        return new GlobalExceptionHandler();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加应用拦截器拦截器
        registry.addInterceptor(serviceAuthRestInterceptor()).addPathPatterns(getIncludePathPatterns()).addPathPatterns("/api/user/validate");

        //添加用户拦截器
        registry.addInterceptor(userAuthRestInterceptor()).addPathPatterns(getIncludePathPatterns());
    }

    /**
     * @description 拦截路径
     * @method  getIncludePathPatterns
     * @param  []
     * @return java.util.ArrayList<java.lang.String>
     * @date: 2019/6/19 15:52
     * @author:zhoujinbing
     */
    private ArrayList<String> getIncludePathPatterns()
    {
        ArrayList<String> objects = new ArrayList<>();
        String[] urls = {
                "/element/**",
                "/gateLog/**",
                "/group/**",
                "/groupType/**",
                "/menu/**",
                "/user/**",
                "/api/permissions",
                "/api/user/un/**"
        };
        Collections.addAll(objects,urls);
        return objects;
    }


    @Bean
    ServiceAuthRestInterceptor serviceAuthRestInterceptor()
    {
        return new ServiceAuthRestInterceptor();
    }

    @Bean
    UserAuthRestInterceptor userAuthRestInterceptor()
    {
        return new UserAuthRestInterceptor();
    }




    @Bean
    public BCryptPasswordEncoder encoder()
    {
        return new BCryptPasswordEncoder(12);
    }

    /**
     * 创建分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor()
    {
        return new PaginationInterceptor();
    }

    /**
     * sql注入器  逻辑删除插件
     * @return
     */
    @Bean
    public ISqlInjector iSqlInjector(){
        return new LogicSqlInjector();
    }

    /**
     * sql性能分析插件，输出sql语句及所需时间
     * @return
     */
    @Bean
    @Profile({"dev","test"})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }
    /**
     * 乐观锁插件
     * @return
     */
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }



}